home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / init.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  9KB  |  357 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: init.c,v 1.19 1996/10/24 15:35:03 aros Exp $
  4.  
  5.     Desc: startup code for AROS (main())
  6.     Lang: english
  7. */
  8. #include <aros/system.h>
  9. #include <stdlib.h>
  10. #include <signal.h>
  11. #ifndef _AMIGA
  12. #define timeval     linux_timeval
  13. #include <sys/time.h>
  14. #undef timeval
  15. #else
  16. #include <sys/time.h>
  17. #endif
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <exec/execbase.h>
  21. #include <exec/memory.h>
  22. #include <exec/devices.h>
  23. #include <clib/aros_protos.h>
  24. #include <clib/exec_protos.h>
  25. #include <dos/dos.h>
  26. #include <dos/dosextens.h>
  27. #include <dos/dostags.h>
  28. #include <clib/dos_protos.h>
  29. #include <utility/tagitem.h>
  30. #include <aros/rt.h>
  31. #include <aros/arosbase.h>
  32. #include "memory.h"
  33. #include <aros/machine.h>
  34. #undef kprintf
  35.  
  36. #define NEWLIST(l)                          \
  37. ((l)->lh_Head=(struct Node *)&(l)->lh_Tail, \
  38.  (l)->lh_Tail=NULL,                         \
  39.  (l)->lh_TailPred=(struct Node *)(l))
  40.  
  41. extern void *ExecFunctions[];
  42. extern const struct Resident Utility_resident;
  43. extern const struct Resident Dos_resident;
  44. extern const struct Resident Graphics_resident;
  45. extern const struct Resident Intuition_resident;
  46. extern const struct Resident emul_handler_resident;
  47. extern const struct Resident Console_resident;
  48.  
  49. #define MEMSIZE 1024*1024
  50. static struct MemHeader mh;
  51. /* static UBYTE memory[MEMSIZE+MEMCHUNK_TOTAL]; */
  52. UBYTE * memory;
  53.  
  54. #define NUMVECT 131
  55.  
  56. struct ExecBase *SysBase;
  57. struct DosLibrary *DOSBase;
  58.  
  59. static int returncode=20;
  60. static struct AROSBase AROSBase;
  61.  
  62. extern struct Task * inputDevice;
  63. extern void debugmem (void);
  64.  
  65. static void idleTask (void)
  66. {
  67.     while (1)
  68.     {
  69.     if (inputDevice)
  70.         Signal (inputDevice, SIGBREAKF_CTRL_F);
  71.  
  72.     Switch (); /* Rescedule */
  73.     }
  74. }
  75.  
  76. int submain(int argc,char *argv[]);
  77.  
  78. static int gargc;
  79. static char **gargv;
  80.  
  81. static void boot(void)
  82. {
  83.     returncode=submain(gargc,gargv);
  84.     RemTask(NULL);
  85. }
  86.  
  87. static void timer (int dummy)
  88. {
  89.     signal (SIGALRM, timer);
  90.  
  91.     if (SysBase->TDNestCnt >= 0
  92.     && SysBase->ThisTask->tc_Node.ln_Pri <=
  93.         ((struct Task *)SysBase->TaskReady.lh_Head)->tc_Node.ln_Pri)
  94.     Switch ();
  95.     else
  96.     SysBase->AttnResched |= 0x80;
  97. }
  98.  
  99. static APTR allocmem(ULONG size)
  100. {
  101.     UBYTE *ret;
  102.  
  103.     size=(size+MEMCHUNK_TOTAL-1)&~(MEMCHUNK_TOTAL-1);
  104.     ret=(UBYTE *)mh.mh_First;
  105.     mh.mh_First=(struct MemChunk *)(ret+size);
  106.     mh.mh_First->mc_Next=NULL;
  107.     mh.mh_Free=mh.mh_First->mc_Bytes=((struct MemChunk *)ret)->mc_Bytes-size;
  108.     return ret;
  109. }
  110.  
  111. void _aros_not_implemented (void)
  112. {
  113.     fprintf (stderr, "This function is not implemented\n");
  114. }
  115.  
  116. int main(int argc,char *argv[])
  117. {
  118.     ULONG * space;
  119.  
  120.     /* Put arguments into globals */
  121.     gargc=argc;
  122.     gargv=argv;
  123.  
  124.     /* Leave a space of 4096 bytes before the memory */
  125.     space = malloc (4096);
  126.     memory = malloc (MEMSIZE+MEMCHUNK_TOTAL);
  127.  
  128.     { /* erase space */
  129.     int size = 4096/sizeof(ULONG);
  130.  
  131.     while (--size)
  132.         *space ++ = 0xDEADBEEF;
  133.     }
  134.  
  135.     /*
  136.     Prepare first MemHeader. I cannot use exec functions
  137.     here because exec is not yet up.
  138.     */
  139.     mh.mh_Node.ln_Name="chip memory"; /* Amiga has always chip, but maybe no fast */
  140.     mh.mh_Node.ln_Pri =0;
  141.     mh.mh_Attributes  =MEMF_CHIP|MEMF_PUBLIC; /* Public to my emulation */
  142.     mh.mh_First=(struct MemChunk *)
  143.         (((IPTR)memory+MEMCHUNK_TOTAL-1)&~(MEMCHUNK_TOTAL-1));
  144.     mh.mh_First->mc_Next=NULL;
  145.     mh.mh_First->mc_Bytes=MEMSIZE;
  146.     mh.mh_Lower=mh.mh_First;
  147.     mh.mh_Upper=(UBYTE *)mh.mh_Lower+MEMSIZE;
  148.     mh.mh_Free =MEMSIZE;
  149.  
  150.     /* The following allocations cannot and must not fail. */
  151.     {
  152.     /* Prepare exec.library */
  153.     ULONG neg,i;
  154.     neg=AROS_ALIGN(LIB_VECTSIZE*NUMVECT);
  155.     SysBase=(struct ExecBase *)
  156.         ((UBYTE *)allocmem(neg+sizeof(struct ExecBase))+neg);
  157.     for(i=1;i<=NUMVECT;i++)
  158.     {
  159.         __AROS_INITVEC(SysBase,i);
  160.         __AROS_SETVECADDR(SysBase,i,ExecFunctions[i-1]);
  161.     }
  162. #if 0
  163.     /* Build GetCC vector (68000 version) */
  164.     ((UWORD *)(__AROS_GETJUMPVEC(SysBase,34))[0]=0x40c0; /* movew sr,d0 */
  165.     ((UWORD *)(__AROS_GETJUMPVEC(SysBase,34))[1]=0x4e75; /* rts         */
  166. #endif
  167.  
  168.     SysBase->LibNode.lib_Node.ln_Name="exec.library";
  169.     SysBase->LibNode.lib_Version = 41;
  170.     SysBase->LibNode.lib_Revision = 9;
  171.  
  172.     SysBase->DebugData = &AROSBase;
  173.  
  174.     AROSBase.kprintf = (void *)kprintf;
  175.  
  176.     NEWLIST(&SysBase->MemList);
  177.     AddHead(&SysBase->MemList,&mh.mh_Node);
  178.     NEWLIST(&SysBase->ResourceList);
  179.     NEWLIST(&SysBase->DeviceList);
  180.     NEWLIST(&SysBase->IntrList);
  181.     NEWLIST(&SysBase->LibList);
  182.     NEWLIST(&SysBase->PortList);
  183.     NEWLIST(&SysBase->TaskReady);
  184.     NEWLIST(&SysBase->TaskWait);
  185.  
  186.     for(i=0;i<5;i++)
  187.     {
  188.         NEWLIST(&SysBase->SoftInts[i].sh_List);
  189.     }
  190.  
  191.     NEWLIST(&SysBase->SemaphoreList);
  192.  
  193.     /* There are no memhandlers yet.
  194.      * (not even the library flushing one which is part of ram/dos.library) */
  195.     NEWLIST((struct List *)&SysBase->ex_MemHandlers);
  196.     SysBase->IDNestCnt=0;
  197.     SysBase->TDNestCnt=0;
  198.     SysBase->AttnResched=0;
  199.     }
  200.  
  201.     {
  202.     /* Add boot task */
  203.     struct Task *t;
  204.     struct MemList *ml;
  205.  
  206.     ml=(struct MemList *)AllocMem(sizeof(struct MemList),MEMF_PUBLIC|MEMF_CLEAR);
  207.     t =(struct Task *)   AllocMem(sizeof(struct Task),   MEMF_PUBLIC|MEMF_CLEAR);
  208.     ml->ml_NumEntries     =1;
  209.     ml->ml_ME[0].me_Addr  =t;
  210.     ml->ml_ME[0].me_Length=sizeof(struct Task);
  211.  
  212.     NEWLIST(&t->tc_MemEntry);
  213.     AddHead(&t->tc_MemEntry,&ml->ml_Node);
  214.     t->tc_Node.ln_Name="Boot task";
  215.     t->tc_Node.ln_Pri=0;
  216.     t->tc_State=TS_RUN;
  217.     t->tc_SigAlloc=0xffff;
  218.     t->tc_SPLower = NULL;        /* This is the system's stack */
  219.     t->tc_SPUpper = (APTR)~0UL; /* all available addresses are ok */
  220.     SysBase->ThisTask=t;
  221.     }
  222.     {
  223.     /* Add idle task */
  224.     struct Task *t;
  225.     struct MemList *ml;
  226.     UBYTE *s;
  227.  
  228.     /* Allocate one header (incl. the first entry) and one additional
  229.         entry */
  230.     ml=(struct MemList *)AllocMem(sizeof(struct MemList)+sizeof(struct MemEntry),
  231.                       MEMF_PUBLIC|MEMF_CLEAR);
  232.     t =(struct Task *)   AllocMem(sizeof(struct Task),    MEMF_PUBLIC|MEMF_CLEAR);
  233.     s =(UBYTE *)         AllocMem(AROS_STACKSIZE,              MEMF_PUBLIC|MEMF_CLEAR);
  234.     ml->ml_NumEntries     =2;
  235.     ml->ml_ME[0].me_Addr  =t;
  236.     ml->ml_ME[0].me_Length=sizeof(struct Task);
  237.     ml->ml_ME[1].me_Addr  =s;
  238.     ml->ml_ME[1].me_Length=AROS_STACKSIZE;
  239.  
  240.     NEWLIST(&t->tc_MemEntry);
  241.     AddHead(&t->tc_MemEntry,&ml->ml_Node);
  242.     t->tc_SPLower=s;
  243.     t->tc_SPUpper=s+AROS_STACKSIZE;
  244.     t->tc_Node.ln_Name="Idle task";
  245.     t->tc_Node.ln_Pri=-128;
  246.     AddTask(t,&idleTask,NULL);
  247.     }
  248.     Enable();
  249.     Permit();
  250.  
  251.     debugmem ();
  252.  
  253.     {
  254.     struct Library * lib;
  255.  
  256.     lib = (struct Library *)InitResident((struct Resident *)&Utility_resident,0);
  257.     if (lib) AddLibrary(lib);
  258.  
  259.     DOSBase = (struct DosLibrary *)InitResident((struct Resident *)&Dos_resident,0);
  260.  
  261.     if (DOSBase) AddLibrary((struct Library *)DOSBase);
  262.  
  263.     lib = (struct Library *)InitResident((struct Resident *)&Graphics_resident,0);
  264.     if (lib) AddLibrary(lib);
  265.     lib = (struct Library *)InitResident((struct Resident *)&Intuition_resident,0);
  266.     if (lib) AddLibrary(lib);
  267.     }
  268.  
  269.     {
  270.     struct consolebase
  271.     {
  272.         struct Device device;
  273.     };
  274.  
  275.     struct consolebase *conbase;
  276.  
  277.     conbase=(struct consolebase *)InitResident((struct Resident *)&Console_resident,0);
  278.     AddDevice (&conbase->device);
  279.     }
  280.  
  281.     DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 39);
  282.  
  283.     if (!DOSBase)
  284.     {
  285.     fprintf (stderr, "Cannot open dos.library");
  286.     exit (10);
  287.     }
  288.  
  289.     {
  290.     struct emulbase
  291.     {
  292.         struct Device eb_device;
  293.         struct Unit *eb_stdin;
  294.         struct Unit *eb_stdout;
  295.         struct Unit *eb_stderr;
  296.     };
  297.  
  298.     struct emulbase *emulbase;
  299.  
  300.     struct TagItem fhtags[]=
  301.     { { TAG_END, 0 } };
  302.  
  303.     struct FileHandle *fh_stdin=(struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,fhtags);
  304.     struct FileHandle *fh_stdout=(struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,fhtags);
  305.  
  306.     struct TagItem bootprocess[]=
  307.     {
  308.         { NP_Entry,     (IPTR)boot },
  309.         { NP_Input,     (IPTR)MKBADDR(fh_stdin) },
  310.         { NP_Output,    (IPTR)MKBADDR(fh_stdout) },
  311.         { NP_Name,        (IPTR)"Boot process" },
  312.         { NP_Cli,        1 },
  313.         { TAG_END, }
  314.     };
  315.  
  316.     emulbase = (struct emulbase *) InitResident (
  317.         (struct Resident *)&emul_handler_resident,
  318.         0
  319.     );
  320.     AddDevice (&emulbase->eb_device);
  321.  
  322.     AssignLock ("C",    Lock ("SYS:c",    SHARED_LOCK));
  323.     AssignLock ("S",    Lock ("SYS:s",    SHARED_LOCK));
  324.     AssignLock ("Libs", Lock ("SYS:libs", SHARED_LOCK));
  325.     AssignLock ("Devs", Lock ("SYS:devs", SHARED_LOCK));
  326.  
  327.     fh_stdin->fh_Device  =&emulbase->eb_device;
  328.     fh_stdin->fh_Unit    =emulbase->eb_stdin;
  329.     fh_stdout->fh_Device =&emulbase->eb_device;
  330.     fh_stdout->fh_Unit   =emulbase->eb_stdout;
  331.  
  332.     /* AROSBase.StdOut = MKBADDR(fh_stdout); */
  333.     AROSBase.StdOut = stderr;
  334.  
  335.     CreateNewProc (bootprocess);
  336.  
  337.     {
  338.         struct itimerval interval;
  339.  
  340.         /* Start Multitasking */
  341.         signal (SIGALRM, timer);
  342.  
  343.         interval.it_interval.tv_sec = interval.it_value.tv_sec = 0;
  344.         interval.it_interval.tv_usec = interval.it_value.tv_usec = 1000000/50;
  345.  
  346.         setitimer (ITIMER_REAL, &interval, NULL);
  347.     }
  348.     }
  349.  
  350.     RemTask(NULL); /* get rid of Boot task */
  351.  
  352.     Switch (); /* Rescedule */
  353.  
  354.     /* Should never get here... */
  355.     return 21;
  356. }
  357.